﻿<UserControl x:Class="FR.SAFTAnalyser.Views.ErrorMessagesView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:local="clr-namespace:FR.SAFTAnalyser.Views">
    <DockPanel>

        <DockPanel.Resources>
            <CollectionViewSource x:Key="error" Source="{Binding ErrorMessages, IsAsync=True}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="DisplayName"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </DockPanel.Resources>

        <TextBlock DockPanel.Dock="Top" Margin="5" Padding="3" Foreground="White"
                   Text="{Binding NumErros}"/>

        <TextBox DockPanel.Dock="Top" Margin="5" Padding="3"
                 Text="{Binding Filtro, UpdateSourceTrigger=PropertyChanged}"/>

        <Button DockPanel.Dock="Bottom" Width="100" Height="25" Margin="5" Padding="3" HorizontalAlignment="Right" ToolTip="Exportar para Excel"
                Command="{Binding DoPrintCommand}"
                CommandParameter="{Binding ElementName=errorMessages}">
            <StackPanel Orientation="Horizontal">
                <Image Source="..\Resources\excel.ico"/>
                <TextBlock Text="Erros" Padding="3"/>
            </StackPanel>
        </Button>
        
        <DataGrid x:Name="errorMessages" DockPanel.Dock="Bottom" VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                  ItemsSource="{Binding Source={StaticResource error}}"
                  SelectedItem="{Binding SelectedError, Mode=TwoWay}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Campo" Width="150" SortMemberPath="Field">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <Hyperlink Command="{Binding DataContext.DoOpenViewCommand, RelativeSource={RelativeSource AncestorType={x:Type local:ErrorMessagesView}}}" Foreground="White">
                                    <TextBlock Name="Field" Text="{Binding Field}" />
                                </Hyperlink>
                            </TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Valor" Binding="{Binding Value}" Width="150"/>
                <DataGridTextColumn Header="Descrição" Binding="{Binding Description}" Width="*" MinWidth="250"/>
            </DataGrid.Columns>

            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Path=Name}" Foreground="{StaticResource Foreground}"/>
                                                    <TextBlock Text=" ( " Foreground="{StaticResource Foreground}"/>
                                                    <TextBlock Text="{Binding Path=ItemCount}" Foreground="{StaticResource Foreground}"/>
                                                    <TextBlock Text=" )" Foreground="{StaticResource Foreground}"/>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>
        
    </DockPanel>
</UserControl>
